package main

import (
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"html/template"
	"myNote/database"
	"myNote/funcs"
	"myNote/middleware"
	"myNote/models"
	"strconv"
)



func main() {
	defer database.DB.Close()
	db := database.DB
	router := gin.Default()
	router.SetFuncMap(template.FuncMap{
		"replaceWrap2Br" : funcs.ReplaceWrap2Br,
		"safe": funcs.Safe,
	})
	router.LoadHTMLGlob("./templates/*")
	router.Static("/layui", "./statics")
	router.Use(middleware.SafeCheck)

	html := router.Group("/html")
	html.GET("/new", func(context *gin.Context) {
		context.HTML(200, "new", nil)
	})
	html.GET("/view", func(context *gin.Context) {
		context.HTML(200, "view", nil)
	})
	html.GET("/operating/:type/:id", func(context *gin.Context) {
		id := context.Param("id")
		doType := context.Param("type")
		article := models.Article{}
		if err := db.Find(&article, "id = ?", id).Error; err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "文章id不存在", "data": nil, "error": err})
			return
		}
		if doType == "edit" {
			context.HTML(200, "edit", map[string]string{"title": article.Title, "text": article.Text, "id": id})
		} else if doType == "detail" {
			context.HTML(200, "detail", map[string]string{"title": article.Title, "text": article.Text})
		}
	})

	api := router.Group("/api")
	api.POST("/article", func(context *gin.Context) {
		var data map[string]interface{}
		if err := context.BindJSON(&data); err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "绑定参数错误", "data": nil, "error": err})
		}
		title := data["title"].(string)
		text := data["text"].(string)
		UserId, _ := context.Get("userId")
		userId, _ := strconv.ParseInt(UserId.(string), 10, 8)
		var newArticle = models.Article{
			Title:  title,
			Text:   text,
			UserId: uint(userId),
		}
		if err := db.Create(&newArticle).Error; err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "新建文章失败", "data": nil, "error": err})
		} else {
			context.JSON(200, gin.H{"code": 200, "msg": "新建文章成功", "data": nil, "error": nil})
		}
	})

	api.PUT("/article", func(context *gin.Context) {
		var data map[string]interface{}
		if err := context.BindJSON(&data); err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "绑定参数错误", "data": nil, "error": err})
		}
		id := data["id"].(string)
		title := data["title"].(string)
		text := data["text"].(string)
		if err := db.Model(&models.Article{}).Where("id = ?", id).Update(map[string]interface{}{"title": title, "text": text}).Error; err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "更新文章失败", "data": nil, "error": err})
		} else {
			context.JSON(200, gin.H{"code": 200, "msg": "更新文章成功", "data": nil, "error": nil})
		}
	})

	api.GET("/article", func(context *gin.Context) {
		var articles []models.Article
		UserId, _ := context.Get("userId")
		userId, _ := strconv.ParseInt(UserId.(string), 10, 8)
		if err := db.Order("created_at desc").Where("user_id = ? ", userId).Find(&articles).Error; err != nil {
			context.JSON(500, gin.H{"code": 500, "msg": "获取文章失败", "data": nil, "error": err})
		} else {
			context.JSON(200, gin.H{"code": 200, "msg": "获取文章成功", "data": articles, "error": nil})
		}
	})

	router.Run(":9528")
}
